<template>
	<section style="width:100%">
		<section>
			<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
				<el-form :inline="true">
					<el-form-item>
						<el-input v-model="userId" placeholder="玩家ID" @change="getUsers(pageSize)"></el-input>
					</el-form-item>
					<el-form-item label="注册开始时间">
						<el-date-picker
								v-model="registerStart"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
					<el-form-item label="注册结束时间">
						<el-date-picker
								v-model="registerEnd"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
					<el-form-item label="登录开始时间">
						<el-date-picker
								v-model="loginStart"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
					<el-form-item label="登录结束时间">
						<el-date-picker
								v-model="loginEnd"
								type="datetime"
								value-format="yyyy-MM-dd HH:mm:ss"
								placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
					<!--<el-form-item label="游戏状态">-->
						<!--<el-select v-model="roomValue" placeholder="请选择">-->
							<!--<el-option-->
									<!--v-for="item in room"-->
									<!--:key="item.value"-->
									<!--:label="item.label"-->
									<!--:value="item.value">-->
							<!--</el-option>-->
						<!--</el-select>-->
					<!--</el-form-item>-->
					<!--<el-form-item label="用户状态">-->
						<!--<el-select v-model="roomValue" placeholder="请选择">-->
							<!--<el-option-->
									<!--v-for="item in room"-->
									<!--:key="item.value"-->
									<!--:label="item.label"-->
									<!--:value="item.value">-->
							<!--</el-option>-->
						<!--</el-select>-->
					<!--</el-form-item>-->
					<!--<el-form-item label="账户类型">-->
						<!--<el-select v-model="roomValue" placeholder="请选择">-->
							<!--<el-option-->
									<!--v-for="item in room"-->
									<!--:key="item.value"-->
									<!--:label="item.label"-->
									<!--:value="item.value">-->
							<!--</el-option>-->
						<!--</el-select>-->
					<!--</el-form-item>-->
					<el-form-item>
						<el-button type="primary" v-on:click="getUsers(pageSize)">查询</el-button>
					</el-form-item>
					<!--<el-form-item>-->
						<!--<el-button type="primary" >禁止提现</el-button>-->
					<!--</el-form-item>-->
					<!--<el-form-item>-->
						<!--<el-button type="primary" >封停账户</el-button>-->
					<!--</el-form-item>-->
					<!--<el-form-item>-->
						<!--<el-button type="primary" >设置银商</el-button>-->
					<!--</el-form-item>-->
				</el-form>

			</el-col>
			<!--列表-->
			<el-table :data="users" stripe highlight-current-row v-loading="listLoading" style="width: 100%;"   :default-sort = "{prop: 'loginTime', order: 'descending'}">
				<!--<el-table-column type="selection" width="55">-->
				<!--</el-table-column>-->
				<el-table-column prop="id" label="玩家ID" fixed width="100" sortable>
				</el-table-column>
				<el-table-column prop="nickname" label="玩家昵称" width="150">
				</el-table-column>
				<el-table-column prop="registerTime" label="注册时间" width="200" sortable>
				</el-table-column>
				<el-table-column prop="loginTime" label="登录时间" width="200" sortable>
				</el-table-column>
				<el-table-column prop="winlose" label="总输赢" width="120" sortable>
					<template slot-scope="scope">
						<span v-if="scope.row.winlose">{{scope.row.winlose}}</span>
						<el-button type="text" size="small" v-else  @click="showWin(scope.row,scope.$index)">显示</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="todaywinlose" label="当日输赢" width="120">
					<template slot-scope="scope">
						<span v-if="scope.row.todaywinlose">{{scope.row.todaywinlose}}</span>
						<el-button type="text" size="small" v-else @click="showTodaywin(scope.row,scope.$index)">显示</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="chargecount" label="充值次数" width="100">
				</el-table-column>
				<el-table-column prop="chargeamount" label="充值金额" width="100">
				</el-table-column>
				<el-table-column prop="withdrawlamount" label="提现金额" width="100">
					<template slot-scope="scope">
						<span v-if="scope.row.withdrawlamount">{{scope.row.withdrawlamount | division}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="amount" label="金币数" width="150" sortable>
					<template slot-scope="scope">
						<span v-if="scope.row.amount">{{scope.row.amount | division}}</span>
						<span v-else>0.00</span>
					</template>
				</el-table-column>
				<el-table-column prop="integralamount" label="奖励点" width="150">
					<template slot-scope="scope">
						<span v-if="scope.row.integralamount">{{scope.row.integralamount}}</span>
						<span v-else>0</span>
					</template>
				</el-table-column>
				<el-table-column
						prop="enabled"
						label="状态/开关"
						width="120"
				>
					<template slot-scope="scope"  >
						<span style="padding-right: 10px">{{scope.row.enabled == '1'?'正常':'停用'}}</span>
						<el-button
								size="mini"
								type="warning"
								@click="handleAdd(scope.row,0)" v-if="scope.row.enabled == '1'" >停用
						</el-button>
						<el-button
								size="mini"
								type="success"
								@click="handleAdd(scope.row,1)" v-if="scope.row.enabled == '0'" >启用
						</el-button>
					</template>
				</el-table-column>
				<el-table-column label="操作" min-width="1000">
					<template slot-scope="scope">
						<el-button type="danger" size="small" @click="editUser(scope.row.id)">修改</el-button>
						<el-button type="danger" size="small" @click="addGold(scope.row.id,scope.row.amount)">增加/扣除金币</el-button>
						<el-button type="danger" size="small" @click="addReward(scope.row.id,scope.row.integralamount)">增加/扣除奖励点</el-button>
						<el-button type="danger" size="small" @click="changeAgent(scope.row.id)">转为代理</el-button>
						<el-button type="primary" size="small" @click="toGold(scope.row.id)">金币变动记录</el-button>
						<el-button type="primary" size="small" @click="toRecharge(scope.row.id)">充值记录</el-button>
						<el-button type="primary" size="small" @click="toPut(scope.row.id)">提现记录</el-button>
						<el-button type="primary" size="small" @click="toReceive(scope.row.id)">赠送接收记录</el-button>
						<!--<el-button type="primary" size="small" >锁定记录</el-button>-->
						<el-button type="primary" size="small" @click="toReward(scope.row.id)">奖励点变动记录</el-button>
						<el-button type="primary" size="small" @click="toChange(scope.row.id)">兑换记录</el-button>
						<!--<el-button type="primary" size="small" >点控记录</el-button>-->
					</template>
				</el-table-column>
			</el-table>

			<!--工具条-->
			<el-col :span="24" class="toolbar">
				<el-pagination layout="sizes, total, prev, pager, next" :page-sizes="[15, 30, 50]" @current-change="handleCurrentChange" @size-change="handleSizeChange"  :page-size="pageSize" :total="total" style="float:right;">
				</el-pagination>
			</el-col>

			<el-dialog title="修改玩家信息" :visible.sync="playerVisible">
				<el-form :model="playerForm" ref="playerForm" label-width="100px" class="demo-dynamic">
					<el-form-item>
						<div style="display: flex;margin-left: -80px">
							<label style="flex:0 0 15%;margin-right: 30px;">属性</label>
							<label style="flex:0 0 50%;margin-right: 30px;">值</label>
							<label style="flex:0 0 5%">操作</label>
						</div>
					</el-form-item>
					<el-form-item v-for="(oldDate,index) in playerForm.oldDate" :key="index">
						<div style="display: flex;margin-left: -80px">
							<label style="flex:0 0 15%;margin-right: 30px;">{{oldDate.field_name}}</label>
							<el-input v-model="oldDate.field_value" style="flex:0 0 50%;margin-right: 30px;"></el-input>
						</div>
					</el-form-item>
					<el-form-item v-for="domain in playerForm.domains">
						<div style="display: flex;margin-left: -80px">
							<el-input v-model="domain.field_name" style="flex:0 0 15%;margin-right: 30px;"></el-input>
							<el-input v-model="domain.field_value" style="flex:0 0 50%;margin-right: 30px;"></el-input>
							<el-button @click.prevent="removeDomain(domain)" style="flex:0 0 5%">删除</el-button>
						</div>
					</el-form-item>
					<el-form-item>
						<div style="margin-left: -80px;text-align: center">
						<el-button type="primary" @click="submitForm" style="width: 100px;">提交</el-button>
						<el-button @click="addDomain" style="width: 100px;">新增</el-button>
						</div>
					</el-form-item>
				</el-form>
			</el-dialog>
			<!--添加扣除金币弹窗-->
			<el-dialog title="添加/扣除金币" :visible.sync="goldVisible">
				<el-form label-width="100px">
					<el-form-item label="原金币数量" >
						<label>{{currentGold | division}}</label>
					</el-form-item>
					<el-form-item label="类型">
						<el-select v-model="currentType" placeholder="请选择">
							<el-option
							v-for="item in currentOptions"
							:key="item.id"
							:label="item.value"
							:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="金币数量">
						<el-input v-model="changeGold" type="number" placeholder="注：正数为增加金币，负数为扣除金币"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="goldVisible = false">取 消</el-button>
					<el-button type="primary" @click="goldSubmit">确 定</el-button>
				</div>
			</el-dialog>
			<!--添加扣除奖励点弹窗-->
			<el-dialog title="添加/扣除奖励点" :visible.sync="rewardVisible">
				<el-form label-width="100px">
					<el-form-item label="原奖励点数量" >
						<label>{{defaultReward}}</label>
					</el-form-item>
					<el-form-item label="奖励点数量">
						<el-input v-model="currentReward" type="number" placeholder="注：正数为增加奖励点，负数为扣除奖励点"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="rewardVisible = false">取 消</el-button>
					<el-button type="primary" @click="rewardSubmit">确 定</el-button>
				</div>
			</el-dialog>
		</section>
	</section>
</template>

<script>
	import base64 from '../../api/base64'

	export default {
		data() {
			return {
                userId:'',
				registerStart:'',
				registerEnd:'',
				loginStart:'',
				loginEnd:'',
                listLoading:false,
				users:[],
                total:0,
				page:1,
                pageSize:15,
                goldVisible:false,
                rewardVisible:false,
                playerVisible:false,
                currentId:'',
				currentGold:'',
				changeGold:'',
				currentType:'',
                currentReward:'',
				defaultReward:'',
				currentPlayerId:'',
				playerForm:{
                    domains: [],
                    oldDate:[]
				},
				currentOptions:[
					{
					    id:'11',
						value:'bug补偿'
					},
                    {
                        id:'12',
                        value:'活动赠送'
                    },
                    {
                        id:'13',
                        value:'账号异常'
                    },
                    {
                        id:'14',
                        value:'其他'
                    }

				]
			}
		},
		methods: {
            keyEnter(key){
                console.log(key)
                if (key.keyCode == 13){
                    this.page = 1;
                    this.getUsers(this.pageSize);
                }
            },
		    //所有跳转
            toGold(agentId){
                sessionStorage.setItem('searchAgentId',agentId)
                this.$router.push({path:'/moneyChange'})
			},
            toRecharge(agentId){
                sessionStorage.setItem('searchAgentId',agentId)
                this.$router.push({path:'/rechargeRecord'})
            },
            toPut(agentId){
                sessionStorage.setItem('searchAgentId',agentId)
                this.$router.push({path:'/cashList'})
            },
            toReceive(agentId){
                sessionStorage.setItem('searchAgentId',agentId)
                this.$router.push({path:'/giveRecord'})
            },
            toReward(agentId){
                sessionStorage.setItem('searchAgentId',agentId)
                this.$router.push({path:'/award'})
            },
            toChange(agentId){
                sessionStorage.setItem('searchAgentId',agentId)
                this.$router.push({path:'/matter'})
            },
            changeAgent(agentId){
                this.$confirm('确认转为代理吗?', '提示', {
                    //type: 'warning'
                }).then(() => {
                    this.$axios.patch('/user/'+agentId+'/to-agent',{},this.global().token).then((res)=>{
                        if (res.status == 200){
                            this.getUsers(this.pageSize);
                        }else{
                            this.$message({message: res.data, type: 'error'});
                        }
                    })
                }).catch(() => {

                });
			},
            handleAdd(row,state){
                this.$axios.patch('/user-enabled/'+row.id,{enabled:state},this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.$message({message: '操作成功', type: 'success'});
                        this.getUsers(this.pageSize);
                    }else{
                        this.$message({message: res.data, type: 'error'});
                    }
                })
			},
            downAgent(){
                this.$router.push({path:'/playerDetail'})
			},
            handleSizeChange(val) {
                this.pageSize = val
                this.getUsers(this.pageSize);
            },
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers(this.pageSize);
			},
			//获取用户列表
			getUsers(page) {
				this.listLoading = true;
                let registerStart = Date.parse(new Date(this.registerStart))
                let registerEnd = Date.parse(new Date(this.registerEnd))
                let loginStart = Date.parse(new Date(this.loginStart))
                let loginEnd = Date.parse(new Date(this.loginEnd))
				if (!registerStart){
                    registerStart = ''
				}
                if (!registerEnd){
                    registerEnd = ''
                }
                if (!loginStart){
                    loginStart = ''
                }
                if (!loginEnd){
                    loginEnd = ''
                }
				this.$axios.get('/users?userid='+this.userId+'&index='+this.page+'&size='+page+'&registeratstart='+registerStart+'&registeratend='+registerEnd+'&lastloginatstart='+loginStart+'&lastloginatend='+loginEnd,{},this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.total = res.data.count
                        let userTable = res.data.data
                        let base = new base64.Base64()
                        userTable.forEach((val)=>{
                            if (val.nickname){
                                val.nickname = base.decode(val.nickname)
                            }
                            // val.todaywinlose = parseFloat(val.todaywinlose)/1000
                            // val.winlose = parseFloat(val.winlose)/1000
                            val.amount = parseFloat(val.amount)
                            if (!(val.registerat== '0' || val.registerat == '')){
                                val.registerTime = this.changeDate(val.registerat)
							}
                            if (!(val.lastloginat== '0' || val.lastloginat == '')){
                                val.loginTime = this.changeDate(val.lastloginat)
                            }
                        })
                        this.users = userTable
                        this.listLoading = false
                    }else{
                        this.$message({message: res.data, type: 'error'});
                        this.listLoading = false
                    }
				})
			},
            addGold(userId,gold){
                this.currentId = userId
				if (gold){
                    this.currentGold = gold
				}else{
                    this.currentGold = 0
				}
                this.goldVisible = true
				this.currentType = '';
                this.changeGold = '';
			},
            addReward(userId,reward){
                this.rewardVisible = true
                this.currentId = userId
                if (reward){
                    this.defaultReward = reward
                }else{
                    this.defaultReward = 0
                }
                this.currentReward = '';
			},
            rewardSubmit(){
               if (this.currentReward == ''){
                    this.$message({message: '请输入奖励点', type: 'error'});
                }else{
                    this.$axios.post('/operate-integral',
                        {
                            userId:this.currentId,
                            amount:parseInt(this.currentReward)
                        }
                        ,this.global().token).then((res)=>{
                        if (res.status == 200){
                            this.$message({message: '操作成功', type: 'success'});
                            this.rewardVisible = false
                            this.getUsers(this.pageSize)
                        }else{
                            this.$message({message: res.data, type: 'error'});
                        }
                    })
                }
			},
			goldSubmit(){
				if (this.currentType == ''){
                    this.$message({message: '请选择类型', type: 'error'});
				}else if (this.changeGold == ''){
                    this.$message({message: '请输入金币', type: 'error'});
				}else{
					this.$axios.post('/operate-coin',
						{
					    	userId:this.currentId,
							type:parseInt(this.currentType),
							amount:parseInt(this.changeGold)*1000
						}
					,this.global().token).then((res)=>{
						if (res.status == 200){
                            this.$message({message: '操作成功', type: 'success'});
                            this.goldVisible = false
							this.getUsers(this.pageSize)
						}else{
                            this.$message({message: res.data, type: 'error'});
						}
					})
				}
			},
            editUser(userId){
                this.playerForm.domains = []
                this.playerVisible = true;
                this.currentPlayerId = userId
                this.$axios.get('/user/'+userId+'/profiles',{},this.global().token).then((res)=>{
                    if(res.status == 200){
                        let base = new base64.Base64()
						let oldDate = res.data
						oldDate.forEach((val)=>{
                            if (val.field_value){
                                val.field_value = base.decode(val.field_value)
                            }
						})
                        this.playerForm.oldDate = oldDate
					}
				})
			},
            addDomain() {
                this.playerForm.domains.push({
                    user_id:this.currentPlayerId,
                    field_name: '',
                    field_value: ''
                });
            },
            submitForm(){
                let userId = this.currentPlayerId;
                let base = new base64.Base64()
                let addDate = this.playerForm.domains
				let newDate = []
                let oldDate = this.playerForm.oldDate
                addDate.forEach((val)=>{
                    if (val.field_value){
                        val.field_value = base.encode(val.field_value)
                    }
                    if (val.field_value== '' || val.field_name == ''){

					}else{
                        newDate.push(val)
					}
                })
                oldDate.forEach((val)=>{
                    if (val.field_value){
                        val.field_value = base.encode(val.field_value)
                    }
                })
                let endData = JSON.stringify(newDate.concat(oldDate))
                this.$axios.patch('/user/'+userId+'/profiles',endData,this.global().token).then((res)=>{
                    this.playerVisible = false;
                    if(res.status == 200){
                        this.$message({message: '修改成功', type: 'success'});
                        this.playerForm.domains = []
                        this.getUsers(this.pageSize)
                    }
                })
			},
            removeDomain(item) {
                var index = this.playerForm.domains.indexOf(item)
                if (index !== -1) {
                    this.playerForm.domains.splice(index, 1)
                }
            },
            showWin(row,index){
                this.$axios.get('/user/'+row.id+'/all-winlose',{},this.global().token).then((res)=>{
					if(res.status == 200){
                        // this.users[index].winlose = (parseFloat(res.data)/1000).toFixed(2)
                    	row.winlose = (parseFloat(res.data)/1000).toFixed(2)
					}
				})
				console.log(this.users)
			},
            showTodaywin(row,index){
                this.$axios.get('/user/'+row.id+'/winlose',{},this.global().token).then((res)=>{
                    if(res.status == 200){
                        row.todaywinlose =  (parseFloat(res.data)/1000).toFixed(2)
                    }
                })
            }
		},
		mounted() {
            this.loginStart = this.Todate1(new Date(new Date()-24*60*60*7000),'yyyy-MM-dd HH:mm:ss')
            this.loginEnd =  this.Todate(new Date(new Date()),'yyyy-MM-dd HH:mm:ss')
			this.getUsers(this.pageSize);
		}
	}

</script>

<style scoped lang="stylus">
</style>